<template>
  <div class="login-wrap">
    <div class="ms-title">图书馆后台管理系统-欢迎登录</div>
    <div class="ms-login">
      <el-form :model="loginForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
        <div v-if="errorInfo" style="margin-bottom: 5px;">
          <span>{{errInfo}}</span>
        </div>
        <el-form-item prop="id">
          <el-input prefix-icon="el-icon-user" v-model="loginForm.id" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" :type="showPassword" placeholder="密码" v-model="loginForm.password"
                    @keyup.enter.native="submitForm()">
            <i slot="suffix" class="el-input__icon el-icon-view" @click="showPwd()"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="validate">
          <el-input v-model="loginForm.validate" class="validate-code" placeholder="验证码"
                    @keyup.enter.native="submitForm()"></el-input>
          <div class="code" @click="refreshCode">
            <s-identify :identifyCode="identifyCode"></s-identify>
          </div>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
  import SIdentify from "./verification";
    export default {
        name: 'login',
        components:{SIdentify},
        data() {
            return {
                identifyCodes: "1234567890",
                identifyCode: "",
                errorInfo: false,
                showPassword:'password',
                loginForm: {
                    id:'',
                    username: '',
                    password: '',
                    validate:'',
                },
                rules: {
                    id: [
                        {required: true, message: '请输入登录名', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'}
                    ],
                    validate: [
                        {required: true, message: '请输入验证码', trigger: 'blur'}
                    ]
                },
            }
        },
        mounted() {
            this.identifyCode = "";
            this.makeCode(this.identifyCodes, 4);
        },
        methods: {
            //是否显示密码
            showPwd(){
                if(this.showPassword === 'text'){
                    this.showPassword = 'password'
                }else {
                    this.showPassword = 'text'
                }
            },
            //登录
            submitForm() {
                const self = this;
                self.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        if (self.loginForm.validate != this.identifyCode) {
                            self.errorInfo = true;
                            self.errInfo = '验证码错误';
                            self.$message.error('验证码错误');
                            this.refreshCode();
                        } else {
                            this.$axios.post('/manage/login/in',this.loginForm).then(res => {
                                if(res.data.code === 0){
                                    const token = res.headers['Authorization']
                                    self.$store.commit('SET_TOKEN',token)
                                    self.$store.commit('SET_USERINFO',res.data.data)
                                    self.$router.push("/")
                                }else {
                                    self.$message.error(res.data.msg)
                                }
                            }).catch(() => {
                                self.$message.error("登录出错了！！！！！")
                            })
                        }
                    } else {
                        console.log('登录出错！！！');
                        return false;
                    }
                });
            },
            //刷新验证码
            refreshCode() {
                this.identifyCode = "";
                this.makeCode(this.identifyCodes, 4);
            },
            makeCode(o, l) {
                for (let i = 0; i < l; i++) {
                    this.identifyCode += this.identifyCodes[
                        this.randomNum(0, this.identifyCodes.length)
                        ];
                }
                console.log(this.identifyCode);
            },
            randomNum(min, max) {
                return Math.floor(Math.random() * (max - min) + min);
            },
        }
    }
</script>

<style scoped>



  .login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-color: #2d3a4b;
    background-image: url("background.jpg");
    background-size: 100% 100%;
  }

  .ms-title {
    position: absolute;
    top: 50%;
    width: 100%;
    margin-top: -230px;
    text-align: center;
    font-size: 30px;
    color: #fff;

  }

  .ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 400px;
    height: 300px;
    margin: -150px 0 0 -190px;
    padding: 40px;
    border-radius: 5px;
    background: #ffffff;
  }

  .ms-login span {
    color: red;
  }

  .login-btn {
    text-align: center;
  }

  .login-btn button {
    width: 100%;
    height: 36px;
  }

  .code {
    width: 112px;
    height: 35px;
    border: 1px solid #ccc;
    float: right;
    border-radius: 2px;
  }

  .validate-code {
    width: 136px;
    float: left;
  }

  .register {
    font-size: 14px;
    line-height: 30px;
    color: #999;
    cursor: pointer;
    float: right;
  }
</style>
